<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;
use think\Db;
use \think\Cookie;
use app\admin\model\Goodstag as GoodstagModel;
class Goodstag extends User
{
    /**
     * 商品标签列表
     */

    public function index()
    {
        $model =new GoodstagModel();
        $post = $this->request->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post=Cookie::get('postlisttag');
        }else{
            //把条件写入缓存
            Cookie::set('postlisttag',$post);
        }
        $where='';
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['tag_name'] = ['like', '%' . $post['keywords'] . '%'];
        }

        if (isset($post['tag_status']) and $post['tag_status'] > 0) {
            $where['tag_status'] = $post['tag_status'];
        }


        $info=$model->where($where)->order('creattime desc')->paginate(20);
        $this->assign('info', $info);
        $this->assign('wherelist', $post);
        return $this->fetch();
    }

    /**
     * 新增、修改TAG
     */

    public function addtag()
    {
        $model = new GoodstagModel();
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id > 0) {
            //是修改操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['tag_name', 'require', '标签名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }

                $basetagname =$model->where('tag_id', $id)->field('tag_name')->find();

                if($basetagname['tag_name']!=$post['tag_name']){
                    //验证昵称是否存在
                    $where['tag_name']=$post['tag_name'];
                    $nickname = $model->where($where)->select();
                    if (!empty($nickname)) {
                        return $this->error('提交失败：该标签名称已被占用');
                    }
                }


                $post['updatetime']=time();

                if (false == $model->allowField(true)->save($post, ['tag_id' => $id])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改标签成功', 'admin/goodstag/index');
                }
            } else {
                //非提交操作
                $info = $model->where('tag_id', $id)->find();
                $this->assign('info', $info);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();

                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['tag_name', 'require', '标签名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }

                $where['tag_name']=$post['tag_name'];
                //验证昵称是否存在
                $tagname = $model->where($where)->select();
                if (!empty($tagname)) {
                    return $this->error('提交失败：该标签名称已被占用');
                }

                $post['creattime']=time();
                $post['updatetime']=time();

                if (false == $model->allowField(true)->save($post)) {
                    return $this->error('添加标签失败');
                } else {
                    return $this->success('添加标签成功', 'admin/goodstag/index');
                }
            } else {
                //非提交操作
                return $this->fetch();
            }
        }
    }


    /**
     * 禁用/启用tag
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $model = new GoodstagModel();
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            $status = $this->request->has('status') ? $this->request->param('status', 0, 'intval') : 0;
            if($status){
                $data['tag_status'] =$status;
            }
            if($status==2){
                $msg='禁用';
            }else{
                $msg='启用';
            }
            $data['updatetime']=time();
            if (false == $model->allowField(true)->save($data, ['tag_id' => $id])) {
                return $this->error($msg.'失败');
            } else {
                return $this->success($msg.'标签成功', 'admin/goodstag/index');
            }
        }
    }



}
